<template>
  <div class="fle" @click="(sw = true), (swt = true)">
    <div class="add-tit">
      <input placeholder="输入文章标题..." v-model="tit" />
      <div class="add_id">
        <span>文章将会被保存至 <span class="bor">草稿</span> </span>
        <span class="ic icon iconfont icon-caogao"></span>
        <span class="issus icon iconfont"
          ><p @click.stop="iss_b">发布 &#xe611;</p>
          <div class="issus_but" :class="sw ? 'show' : ''" @click.stop>
            <p class="iss-tit">发布文章</p>
            <p>分类</p>
            <span :class="x == '后端' ? 'affg' : ''" @click="ck('后端')"
              >后端</span
            >
            <span :class="x == '前端' ? 'affg' : ''" @click="ck('前端')"
              >前端</span
            >
            <span :class="x == 'Android' ? 'affg' : ''" @click="ck('Android')"
              >Android</span
            >
            <span :class="x == 'iOs' ? 'affg' : ''" @click="ck('iOs')"
              >iOs</span
            >
            <span :class="x == '人工智能' ? 'affg' : ''" @click="ck('人工智能')"
              >人工智能</span
            >
            <br /><span
              :class="x == '开发工具' ? 'affg' : ''"
              @click="ck('开发工具')"
              >开发工具</span
            >
            <span :class="x == '代码人生' ? 'affg' : ''" @click="ck('代码人生')"
              >代码人生</span
            >
            <span :class="x == '阅读' ? 'affg' : ''" @click="ck('阅读')"
              >阅读</span
            >
            <p>添加标签</p>
            <span>+添加标签</span>
            <p><span class="aff" @click="publish">确认并发布</span></p>
          </div>
        </span>
        <span class="id-img" @click.stop="swt = !swt"
          ><img
            :src="'http://api_devss.wanxikeji.cn/' + this.$store.state.icon"
          />
          <ul :class="swt ? 'show' : ''">
            <li>写文章</li>
            <li>草稿</li>
            <li></li>
            <router-link :to="{path:`/user/${$store.state.userID}`}" tag="li">我的主页</router-link>
            <li>我喜欢的</li>
            <li>我的收藏集</li>
            <li @click="goback">返回主页</li>
            <li></li>
            <li>设置</li>
            <li>关于</li>
          </ul>
        </span>
      </div>
    </div>
    <div class="add-det">
      <div ref="re" class="det_left" contentEditable="true" @input="mon">
        ---<br />
        <span class="left-list"
          ># 主题列表：juejin, github, smartblue, cyanosis, channing-cyan,
          fancy, hydrogen, condensed-night-purple , greenwillow, v-green,
          vue-pro, healer-readable, mk-cute, jzman, geek-black, awesome-green,
          qklhk-chocolate<br />
          # 贡献主题：https://github.com/xitu/juejin-markdown-themes </span
        ><br />
        <span class="det-t">
          theme: <span class="bk"> juejin</span><br />
          highlight:<br />
          ---
        </span>
      </div>
      <textarea v-model="art"></textarea>
    </div>
    <div class="add-m">
      <div>
        <input type="file" class="input-img" @change="hand" />
        <span class="ic-img icon iconfont icon-caogao"></span>了解社区规范
      </div>
      <div>字数{{ leng }}</div>
    </div>
  </div>
</template>
<script>
export default {
  data() {
    return {
      art: "", //内容input双向绑定
      leng: 0, //字数
      x: "",
      sw: true,
      swt: true,
      tit: "", //标题
      info: "",
      info1: {
        content: "",
        imgs: [],
      },

      img: "",
      aryimg: [],
    };
  },
  methods: {
    goback() {
      this.$router.push("/index");
    },
    mon() {
      this.art = this.$refs.re.innerText;
      this.info1.content = this.art;
      this.info = JSON.stringify(this.info1);
      this.leng = this.art.length;
    },
    ck(val) {
      this.x = val;
    },
    iss_b() {
      this.sw = !this.sw;
    },
    publish() {
      if (this.tit !== "" && this.x !== "") {
        this.axios({
          method: "post",
          url: "api/addModifyArticle",
          data: {
            token: this.$store.state.token,
            title: this.tit,
            info: this.info,
            tags_name: [this.x],
          },
        })
          .then((res) => {
            console.log(res);

            alert("发布成功！卧龙兄，掘铁有你会变得更好");
            this.$router.push("/index");
          })
          .catch((err) => {
            console.log(err);
            alert("发布失败");
          });
      } else {
        alert("请输入标题和分类后再提交哦");
      }
    },
    hand(e) {
      let file = e.target || e.srcElement;

      let fil = file.files[0];
      var reader = new FileReader();
      reader.readAsDataURL(fil);
      let param = new FormData();
      param.append("img", fil);

      this.axios({
        method: "post",
        url: "api/savePic",
        data: param,
      })
        .then((res) => {
          console.log(res);
          this.info1.content = this.art;
          this.info1.imgs.push(res.data.data);
          this.info = JSON.stringify(this.info1);
          console.log(this.info);
          let wu = "http://api_devss.wanxikeji.cn/" + res.data.data;
          this.img = `<img style=" max-height: 200px;max-width: 200px;" src="${wu}">`;
          this.$refs.re.innerHTML += this.img;
          // this.mon()
        })
        .catch((err) => {
          alert("选择图片失败");
          console.log(err);
        });
    },
  },
};
</script>
<style scoped>
html,
body {
  padding: 0;
  margin: 0;
  height: 100%;
}
.input-img {
  position: absolute;
  width: 30px;
  left: 20px;
  top: 20px;
  opacity: 0;
}
.ic-img {
  font-size: 30px;
}
.fle {
  width: 100%;
  height: 930px;
  display: flex;
  flex-direction: column;
}
.add-tit {
  min-width: 600px;
  height: 60px;
  display: flex;
  justify-content: space-between;
  padding: 10px;
  border: 1px solid rgb(204, 211, 218);
  box-sizing: border-box;
  background: white;
  line-height: 40px;
  color: rgb(204, 211, 218);
}
.iss-tit {
  font-size: 25px;
  font-weight: bold;
}
.add-tit span {
  cursor: pointer;
}
.add-tit input {
  border: none;
  outline: none;
  font-size: 25px;
  font-weight: bold;
}
.add_id {
  display: flex;
  justify-content: space-between;
}
.add_id img {
  width: 40px;
  height: 40px;
  border-radius: 50%;
}
.id-img {
  width: 40px;
  height: 40px;
  border-radius: 50%;

  background-image: url(../../assets/img/user_avatar/avatar.png);
  background-size: 40px 40px;
  position: relative;
}
.id-img ul {
  position: absolute;
  top: 48px;
  padding: 10px 0;
  border: 1px solid rgb(204, 211, 218);
  right: -11px;
  color: rgb(177, 177, 180);
  background: white;
}
.id-img ul li {
  width: 130px;
  height: 30px;
  padding: 0px 20px;
  line-height: 30px;
}
.id-img ul li:hover {
  background: #f7f5f5;
}
.ic {
  width: 80px;
  height: 60px;
  text-align: center;
  font-size: 20px;
}
.issus {
  width: 80px;
  height: 60px;

  color: #007fff;
  position: relative;
}
.issus .issus_but::after {
  content: "";
  width: 20px;
  height: 20px;
  border-top: 1px solid rgb(204, 211, 218);
  border-left: 1px solid rgb(204, 211, 218);
  background: white;
  position: absolute;
  top: -11px;
  right: 30px;
  transform: rotate(45deg);
}
.issus_but {
  position: absolute;
  padding: 20px;
  right: 0;
  top: 48px;
  background: white;
  width: 300px;
  height: 300px;
  border: 1px solid rgb(204, 211, 218);
  color: #909090;
}
.issus_but span {
  font-size: 14px;
  border: 1px solid rgb(225, 229, 233);
  padding: 5px;
  margin-right: 5px;
  border-radius: 5px;
}
.issus_but span:hover {
  border: 1px solid #007fff;
  color: #007fff;
}
.issus_but .aff {
  float: right;
  padding: 10px;
  line-height: 20px;
  border: 1px solid #007fff;
  color: #007fff;
  border-radius: 5px;
}
.issus_but .affg {
  border: 1px solid #007fff;
  color: #007fff;
}
.icon {
  overflow: visible;
}
.bor {
  padding: 5px 10px;
  border: 1px solid rgb(232, 235, 238);
}
.fle .add-det {
  flex: 1;
  display: flex;
  height: 500px;
  justify-content: space-between;
}
.add-det textarea {
  width: 50%;

  border: 1px solid rgb(204, 211, 218);
  box-sizing: border-box;
  outline: none;
  padding: 30px;
  resize: none;
}
.det_left {
  width: 50%;

  border: 1px solid rgb(204, 211, 218);
  box-sizing: border-box;
  padding: 30px;
  outline: none;
  font-size: 14px;
  line-height: 25px;
}
.det_left img {
  max-height: 300px;
  max-width: 300px;
}
.left-list {
  color: #009e9d;
}
.det-t {
  color: #219;
}
.bk {
  color: black;
}
.add-m {
  width: 100%;
  height: 60px;
  display: flex;
  justify-content: space-around;
  line-height: 60px;
  position: relative;
}
.add-m div {
  padding: 0 20px;
  width: 50%;
  background: white;
  border: 1px solid rgb(204, 211, 218);
  box-sizing: border-box;
  color: #909090;
}
.add-m div:last-child {
  text-align: right;
}
.show {
  display: none;
}
</style>